package com.aat.dxfy.game.blankwhite;

public class ThreadTest2 implements Runnable {

	private static int ticket = 10;

	private static int a = 0;

	public static void main(String[] args) {

		ThreadTest2 thread = new ThreadTest2();

		Thread t1 = new Thread(thread, "1号窗口卖票");
		Thread t2 = new Thread(thread, "2号窗口卖票");
		Thread t3 = new Thread(thread, "3号窗口卖票");
		Thread t4 = new Thread(thread, "4号窗口卖票");
		Thread t5 = new Thread(thread, "5号窗口卖票");
		Thread t6 = new Thread(thread, "6号窗口卖票");
		Thread t7 = new Thread(thread, "7号窗口卖票");
		Thread t8 = new Thread(thread, "8号窗口卖票");
		Thread t9 = new Thread(thread, "9号窗口卖票");
		Thread t10 = new Thread(thread, "10号窗口卖票");
		Thread t11 = new Thread(thread, "11号窗口卖票");
		Thread t12 = new Thread(thread, "12号窗口卖票");
		Thread t13 = new Thread(thread, "13号窗口卖票");
		Thread t14 = new Thread(thread, "14号窗口卖票");
		Thread t15 = new Thread(thread, "15号窗口卖票");
		Thread t16 = new Thread(thread, "16号窗口卖票");

		t1.start();
		t2.start();
		t3.start();
		t4.start();
		t5.start();
		t6.start();
		t7.start();

		t8.start();
		t9.start();
		t10.start();
		t11.start();
		t12.start();
		t13.start();
		t14.start();
		t15.start();
		t16.start();

	}

	public void run() {
		if (ThreadTest2.ticket > 0) {
			sal();
		} else {
			a++;
			if (a < 3) {
				add();
			}

		}
	}

	public synchronized void sal() {
		if (ThreadTest2.ticket > 0) {
			System.out.println("[售]当前的票数：" + ThreadTest2.ticket);
			ThreadTest2.ticket--;
			System.out.println("----" + Thread.currentThread().getName()
					+ "，卖票1张，剩余：" + ThreadTest2.ticket + "张...");
			System.out.println("/------------------------------/");
		} else {
			System.out.println("票已售完！");
		}
	}

	public synchronized void add() {
		ThreadTest2.ticket = ThreadTest2.ticket + 10;
		System.out.println("系统加了10张票，当前票数：" + ThreadTest2.ticket);
	}

}